package edu.pku.sei.modeler.neditor.command;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.notation.core.ShapeNode;
import edu.pku.sei.modeler.model.notation.shape.Shape;
import edu.pku.sei.modeler.neditor.model.Diagram;

public class RedirectedChangeConstraintCommand extends Command {
	private Rectangle constraint;
	private MetaModelElement target;
	private MetaModelElement child;
	private MetaModelElement parent;

	@Override
	public boolean canExecute() {
		return true;
	}

	private Rectangle oldConstraint = new Rectangle();
	
	@Override
	public void execute() {
		Rectangle tarR = ((ShapeNode)target).getBound();
		constraint.translate(tarR.x, tarR.y);
		oldConstraint.setBounds(((ShapeNode)child).getBound());
		if(parent instanceof Diagram){
			((ShapeNode)child).setBound(constraint);
		} else {
			((Shape)parent).changeChildBound((ShapeNode)child, constraint);
		}
	}

	@Override
	public void redo() {
		// TODO Auto-generated method stub
		if(parent instanceof Diagram){
			((ShapeNode)child).setBound(constraint);
		} else {
			((Shape)parent).changeChildBound((ShapeNode)child, constraint);
		}
	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub
		if(parent instanceof Diagram){
			((ShapeNode)child).setBound(oldConstraint);
		} else {
			((Shape)parent).changeChildBound((ShapeNode)child, oldConstraint);
		}
	}

	public void setChild(MetaModelElement child) {
		this.child = child;
	}


	public void setConstraint(Rectangle constraint) {
		this.constraint = constraint;
	}

	public void setParent(MetaModelElement parent) {
		this.parent = parent;
	}

	public void setTarget(MetaModelElement target) {
		this.target = target;
	}

}
